package com.example.ribbonconsumer.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.example.ribbonconsumer.model.Book;
import com.example.ribbonconsumer.model.User;
import com.example.ribbonconsumer.service.UserService;
import com.example.ribbonconsumer.service.impl.BookService;
import com.example.ribbonconsumer.service.impl.HelloService;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;

@RestController
public class ConsumerController {
	
	@Autowired
	RestTemplate restTemplate;
	
	@Autowired
	private HelloService helloService;
	
	@Autowired
	private BookService bookService;
	
	@Autowired
	private UserService userService;
	
	@RequestMapping(value="/ribbon-consumer",method=RequestMethod.GET)
	public String helloConsumer(){
		//return restTemplate.getForEntity("http://HELLO-SERVICE/hello", String.class).getBody();
		return helloService.helloService();
	}
	
	@RequestMapping(value="/getbookbyname",method=RequestMethod.GET)
	public Book bookConsumer(){
		//三种方式：
		//第一种：参数绑定
		//return restTemplate.getForEntity("http://HELLO-SERVICE/getbookbyname?name={1}", Book.class, "didi").getBody();
		//第二种：Map
		/*Map<String, String> params = new HashMap<>();
		params.put("name", "didi");
		return restTemplate.getForEntity("http://HELLO-SERVICE/getbookbyname?name={name}", Book.class, params).getBody();*/
		//第三种：URI
		/*UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://HELLO-SERVICE/getbookbyname?name={name}")
		.build().expand("didi").encode();
		URI uri = uriComponents.toUri();*/
		/*return restTemplate.getForObject(uri, Book.class);*/
		
		//return restTemplate.getForObject(uri, Book.class);
		//restTemplate.getForObject("http://HELLO-SERVICE/getbookbyname?name={1}", Book.class, "didi");
		/*HystrixRequestContext.initializeContext();//初始化请求上下文
		BookCommand bookCommand = new BookCommand(restTemplate,"didi");*/
		/*return bookCommand.execute();*/
		HystrixRequestContext.initializeContext();
		bookService.getBookByName("didi");
		return bookService.getBookByName("didi");
		
	}
	
	@RequestMapping(value="/postbookbyname",method=RequestMethod.GET)
	public Book getBookByPost(){
		//第一种
		//return restTemplate.postForEntity("http://HELLO-SERVICE/postbookbyname","didi",Book.class).getBody();
		return restTemplate.postForObject("http://HELLO-SERVICE/postbookbyname","didi",Book.class);
	 	//URI responseURI = restTemplate.postForLocation("http://HELLO-SERVICE/postbookbyname","didi");
	}

	@RequestMapping(value="/updatebookbyname",method=RequestMethod.GET)
	public void updateBookByName(){
		Book book = new Book();
		book.setName("didi");
		bookService.updateBook(book);
	}
	
	@RequestMapping(value = "/users/{id}",method=RequestMethod.GET)
	public User getUserById(@PathVariable Long id){
		return userService.find(id);
	}
	
	@RequestMapping(value = "/users",method=RequestMethod.GET)
	public List<User> getUserById(@RequestParam List<Long> ids){
		return userService.findAll(ids);
	}
	
	
}
